part of '../../repository.dart';

const _kGooglePlayPurchasedSubscriptionTransactionExpirationTimeKey = 'expiration_time';
const _kGooglePlayPurchasedSubscriptionTransactionOfferTypeKey = 'offer_type';

class _GooglePlayPurchasedSubscriptionTransactionDto extends Equatable
    implements _GooglePlayTransactionDto {
  const _GooglePlayPurchasedSubscriptionTransactionDto._internal({
    required this.orderId,
    required this.timeDto,
    required this.expirationTimeDto,
    required this.offerTypeDto,
    required this.revocationInfoDto,
  });

  @override
  final String orderId;

  @override
  final ApiTimeDto timeDto;

  @override
  final _GooglePlayTransactionRevocationInfoDto? revocationInfoDto;

  final ApiTimeDto expirationTimeDto;
  final _GooglePlayPurchasedSubscriptionTransactionOfferTypeDto offerTypeDto;

  @override
  List<Object?> get props {
    return [
      orderId,
      timeDto,
      expirationTimeDto,
      offerTypeDto,
      revocationInfoDto,
    ];
  }

  factory _GooglePlayPurchasedSubscriptionTransactionDto.fromJson(Map<String, dynamic> json) {
    final expirationTimeString =
        json[_kGooglePlayPurchasedSubscriptionTransactionExpirationTimeKey] as String;
    final expirationTimeDto = ApiTimeDto.fromString(expirationTimeString);

    final offerTypeString =
        json[_kGooglePlayPurchasedSubscriptionTransactionOfferTypeKey] as String;
    final offerTypeDto =
        _GooglePlayPurchasedSubscriptionTransactionOfferTypeDto.fromString(offerTypeString);

    final timeString = json[_kGooglePlayTransactionTimeKey] as String;
    final timeDto = ApiTimeDto.fromString(timeString);

    final revocationInfoJson =
        json[_kGooglePlayTransactionRevocationInfoKey] as Map<String, dynamic>?;

    _GooglePlayTransactionRevocationInfoDto? revocationInfoDto;

    if (revocationInfoJson != null) {
      revocationInfoDto = _GooglePlayTransactionRevocationInfoDto.fromJson(revocationInfoJson);
    }

    return _GooglePlayPurchasedSubscriptionTransactionDto._internal(
      orderId: json[_kGooglePlayTransactionOrderIdKey],
      expirationTimeDto: expirationTimeDto,
      timeDto: timeDto,
      offerTypeDto: offerTypeDto,
      revocationInfoDto: revocationInfoDto,
    );
  }

  @override
  GooglePlayPurchasedSubscriptionTransaction toEntity() {
    return GooglePlayPurchasedSubscriptionTransaction(
      orderId: orderId,
      expirationTime: expirationTimeDto.toEntity(),
      offerType: offerTypeDto.toEntity(),
      time: timeDto.toEntity(),
      revocationInfo: revocationInfoDto?.toEntity(),
    );
  }
}
